var PROJECT_PATH = "";
function setPathPrefix(path) {
	PROJECT_PATH = path;
}
var $projectList = $('#projectList');
$(function() {
	// 注册清空事件
	WASP_WIDGET.registerResetClearEvent();
	
    $projectList.jqGrid({
        url: PROJECT_PATH+'projectManagerList.do',
        datatype: "json",        
		colNames:["产品代码","产品简称",'操作'],        
		colModel:[
                    {name:'prjcode',index:'prjcode',width:30,align:'left', key:true,sortable:false},    
                    {name:'prjsname',index:'prjsname', width:60, resizable:true,align:'left',sortable:false},
					{name:'option',index:'option', width: 20, resizable:true,align:'left',sortable:false}
				 ],     
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: false,
        pager: "#projectPage",
        viewrecords: true,
        hidegrid: false,
        gridComplete: function() {
            var ids = $projectList.jqGrid('getDataIDs');
				for(var i=0;i < ids.length;i++){
					var rowData = $projectList.jqGrid('getRowData', ids[i]); 
                    var prjcode = rowData.prjcode;
                    var prjsname = rowData.prjsname;
                    be = '<a class="btn btn-link btn-jqgrid" title="选择" onclick="WASP_PRODUCT.selectProject(\''+prjcode+'\',\''+prjsname+'\');" ><i class="fa fa-plus-circle"></i></a>';
                    $projectList.jqGrid('setRowData',ids[i],{option:be});
				}	
			}				
		});
    $projectList.navGrid('#projectPage', { edit: false, add: false, del: false, search: false, refreshstate: 'current' });
    $projectList.jqGrid('setFrozenColumns');
    jqGridResize($projectList);
});

var WASP_PRODUCT = {
	selectProject : function (prjcode,prjsname){
		if(window.opener != null){
			if(window.opener.selectProjectCallback != null) {
				window.opener.selectProjectCallback(prjcode,prjsname);
				window.close();
		    } else {
		    	ctools.alert_sweet("错误",'error', "父页面未定义回调方法，无法添加选择的产品，点击关闭将关闭本页面", function(){
					window.close();
				});
		    }
		} else {
			ctools.alert_sweet("错误",'error', "父页面已关闭，无法添加指定产品，点击关闭将关闭本页面", function(){
				window.close();
			});
		}
	}
} 

/**
 * 通过条件进行搜索
 */
function queryByCondtion(flag) {
	debugger;
	//产品代码
	var prjCode = $.trim($("#q-prjCode").val());
	//产品简称
	var prjsname = $.trim($("#q-prjsname").val());
	
    var postData = $projectList.jqGrid("getGridParam", "postData");
    $.extend(postData, {
        'sp[prjCode]':prjCode,
        'sp[prjsname]':prjsname,
    });
    if (flag) {
    	$projectList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$projectList.trigger("reloadGrid"); //重新载入Grid表格
    }
};

//对按钮绑定全屏事件
$(document).on('click', '.btn-fullscreen', function(){
    var $wrapper = $(this).parents('.fullscreen-wrapper')[0];
    toggleFullScreen(document.documentElement);
    //全屏的时候将几个模态框放到下面去
    $('.modal[role="dialog"]').appendTo($wrapper);
});
